import { search } from 'api/search'
import { detail } from 'api/user'
import { numberFormat } from '@/utils'

class Artist {
  constructor(name) {
    this.name = name
  }

  getArtistDetail() {
    return new Promise((resolve) => {
      search({
        keywords: this.name,
        type: 100
      }).then(res => {
        const { result: { artists } } = res
        // let artist = artists[0]

        let artist = artists.find(item => {
          return item.name === this.name
        })
  
        if (artist) {
          const { picUrl, accountId, id, albumSize, mvSize, followed, img1v1Url } = artist
          this.picUrl = picUrl
          this.img1v1Url = img1v1Url
          this.accountId = accountId
          this.id = id
          this.albumSize = albumSize // 专辑数量
          this.mvSize = mvSize // mv数量
          this.followed = followed // 是否关注

          if (accountId) {
            detail(accountId).then(res => {
              const { profile } = res
              const { avatarUrl, followeds, follows, city, allAuthTypes } = profile
              this.avatarUrl = avatarUrl // 头像
              this.followeds = numberFormat(followeds) // 粉丝数
              this.follows = follows // 关注
              this.city = city // 城市
              this.profile = profile

              this.allAuthTypes = allAuthTypes
  
              resolve(this)
            })
          } else {
            resolve(this)
          }
        }
      })
    })
  }
}

export default Artist
